using System;
using System.Collections;
using BitMango.Diagnostics;
using Holoville.HOTween;
using UnityEngine;

public class IOSSwitch : tk2dUIBaseItemControl
{
	public bool UseOnReleaseInsteadOfOnUp
	{
		get
		{
			return this.useOnReleaseInsteadOfOnUp;
		}
	}

	public bool IsOn
	{
		get
		{
			return this.isOn;
		}
		set
		{
			if (this.tweener != null && !this.tweener.isComplete)
			{
				return;
			}
			if (value == this.isOn)
			{
				return;
			}
			this.isOn = value;
			this.AnimateSwitchHandler(this.isOn);
		}
	}

	private void Awake()
	{
		this.InitData();
	}

	private void Start()
	{
		if (this.useOnOverlay)
		{
			Debugger.Assert(Scene.TopOverlayCamera != null, "You don't have Overlay Camera.");
			this.cam = Scene.TopOverlayCamera;
		}
		else if (Scene.CurrentPopup() != null)
		{
			this.cam = Scene.PopupCamera;
		}
		else if (Scene.CurrentScene() != null)
		{
			this.cam = Scene.SceneCamera;
		}
	}

	private void Update()
	{
		this.TrackingTouchForSwitch();
	}

	private void OnEnable()
	{
		if (this.uiItem)
		{
			this.uiItem.OnDown += this.ButtonDown;
			if (this.canButtonBeHeldDown)
			{
				if (this.useOnReleaseInsteadOfOnUp)
				{
					this.uiItem.OnRelease += this.ButtonUp;
				}
				else
				{
					this.uiItem.OnUp += this.ButtonUp;
				}
			}
		}
		this.internalTweenInProgress = false;
		this.tweenTimeElapsed = 0f;
		this.handler.dimensions = this.handlerSmallSize;
	}

	private void OnDisable()
	{
		if (this.uiItem)
		{
			this.uiItem.OnDown -= this.ButtonDown;
			if (this.canButtonBeHeldDown)
			{
				if (this.useOnReleaseInsteadOfOnUp)
				{
					this.uiItem.OnRelease -= this.ButtonUp;
				}
				else
				{
					this.uiItem.OnUp -= this.ButtonUp;
				}
			}
		}
	}

	private void ButtonDown()
	{
		this.isPressed = true;
		this._handlerScaleTweenSec = this.handlerScaleTweenSec;
		if (this._handlerScaleTweenSec <= 0f)
		{
			this.handler.dimensions = this.handlerWideSize;
		}
		else
		{
			this.handler.dimensions = this.handlerSmallSize;
			this.tweenTargetScale = this.handlerWideSize;
			this.tweenStartingScale = this.handler.dimensions;
			if (!this.internalTweenInProgress)
			{
				base.StartCoroutine(this.ScaleTween());
				this.internalTweenInProgress = true;
			}
		}
		this.AnimateBgFrontToDownSize(true);
	}

	private void ButtonUp()
	{
		this.isPressed = false;
		this.startTouchPos = IOSSwitch.OUTOFSCREEN_POS;
		this.FixHandlerColliderBounds();
		this.handler.GetComponent<tk2dUIItem>().sendMessageTarget = base.gameObject;
		this._handlerScaleTweenSec = this.handlerScaleTweenSec * 8f;
		if (this._handlerScaleTweenSec <= 0f)
		{
			this.handler.dimensions = this.handlerSmallSize;
			this.handler.anchor = ((!this.isOn) ? this.anchorML : this.anchorMR);
			Vector3 localPosition = this.handlerPos;
			localPosition.x = ((!this.isOn) ? (-this.handlerSmallPosX) : this.handlerSmallPosX);
			this.handler.transform.localPosition = localPosition;
		}
		else
		{
			this.tweenTargetScale = this.handlerSmallSize;
			this.tweenStartingScale = this.handler.dimensions;
			if (!this.internalTweenInProgress)
			{
				base.StartCoroutine(this.ScaleTween());
				this.internalTweenInProgress = true;
			}
		}
		if (this.isOn)
		{
			return;
		}
		this.AnimateBgFrontToDownSize(false);
	}

	public void InitWithOnState(bool enable = false)
	{
		this.isOn = !enable;
		this.IsOn = enable;
		this.AnimateBgFrontToDownSize(enable);
		HOTween.Complete("iOSSwitch");
	}

	public void Switch()
	{
		this.isOn = !this.isOn;
		this.AnimateSwitchHandler(this.isOn);
	}

	private void TrackingTouchForSwitch()
	{
		if (this.isPressed)
		{
			IOSSwitch.SWITCH_STATE switchState = this.GetSwitchState();
			if (this.IsOn && switchState == IOSSwitch.SWITCH_STATE.ON)
			{
				return;
			}
			if (!this.IsOn && switchState == IOSSwitch.SWITCH_STATE.OFF)
			{
				return;
			}
			if (switchState != IOSSwitch.SWITCH_STATE.NOTHING)
			{
				if (switchState != IOSSwitch.SWITCH_STATE.ON)
				{
					if (switchState == IOSSwitch.SWITCH_STATE.OFF)
					{
						this.IsOn = false;
					}
				}
				else
				{
					this.IsOn = true;
				}
			}
		}
	}

	private IOSSwitch.SWITCH_STATE GetSwitchState()
	{
		if (this.cam == null)
		{
			if (this.useOnOverlay)
			{
				Debugger.Assert(Scene.TopOverlayCamera != null, "You don't have Overlay Camera.");
				this.cam = Scene.TopOverlayCamera;
			}
			else if (Scene.CurrentPopup() != null)
			{
				this.cam = Scene.PopupCamera;
			}
			else if (Scene.CurrentScene() != null)
			{
				this.cam = Scene.SceneCamera;
			}
		}
		Vector3 vector = this.cam.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
		if (this.startTouchPos == IOSSwitch.OUTOFSCREEN_POS)
		{
			this.startTouchPos = vector;
		}
		if (Mathf.Abs(vector.x - this.startTouchPos.x) <= 0.3f)
		{
			return IOSSwitch.SWITCH_STATE.NOTHING;
		}
		if (vector.x - base.transform.position.x > 0f)
		{
			this.handler.GetComponent<tk2dUIItem>().sendMessageTarget = null;
			return IOSSwitch.SWITCH_STATE.ON;
		}
		if (vector.x - base.transform.position.x < 0f)
		{
			this.handler.GetComponent<tk2dUIItem>().sendMessageTarget = null;
			return IOSSwitch.SWITCH_STATE.OFF;
		}
		return IOSSwitch.SWITCH_STATE.NOTHING;
	}

	private void AnimateBgFrontToDownSize(bool state)
	{
		if (!this.showOffScaleTween)
		{
			return;
		}
		Vector3 vector = (!state) ? Vector3.one : Vector3.zero;
		TweenParms p_parms = new TweenParms().Prop("localScale", vector).Ease(EaseType.EaseOutQuad);
		HOTween.To(this.bgFront.transform, 0.3f, p_parms);
	}

	private void AnimateSwitchHandler(bool _isOn)
	{
		Vector3 pos = this.handlerPos;
		pos.x = ((!_isOn) ? (-this.handlerWidePosX) : this.handlerWidePosX);
		TweenParms p_parms = new TweenParms().Prop("localPosition", pos).Id("iOSSwitch").Ease(EaseType.EaseInOutQuad).OnStart(delegate()
		{
			this.FixHandlerColliderBounds();
		}).OnComplete(delegate()
		{
			this.handler.anchor = ((!_isOn) ? this.anchorML : this.anchorMR);
			pos.x = ((!_isOn) ? (-this.handlerSmallPosX) : this.handlerSmallPosX);
			this.handler.transform.localPosition = pos;
			this.FixHandlerColliderBounds();
		});
		this.tweener = HOTween.To(this.handler.transform, this.handlerMovingTweenSec, p_parms);
		p_parms = new TweenParms().Prop("color", (!_isOn) ? this.bgBackOffColor : this.bgBackOnColor).Ease(EaseType.EaseInOutQuad);
		HOTween.To(this.bgBack, 0.15f, p_parms);
	}

	private void FixHandlerColliderBounds()
	{
		Vector3 vector = this.handler.gameObject.GetComponent<Renderer>().bounds.center;
		vector -= this.handler.transform.position;
		this.handler.GetComponent<BoxCollider>().center = vector;
	}

	private IEnumerator ScaleTween()
	{
		this.tweenTimeElapsed = 0f;
		while (this.tweenTimeElapsed < this._handlerScaleTweenSec)
		{
			this.handler.dimensions = Vector2.Lerp(this.tweenStartingScale, this.tweenTargetScale, this.tweenTimeElapsed / this._handlerScaleTweenSec);
			yield return null;
			this.tweenTimeElapsed += tk2dUITime.deltaTime;
		}
		this.handler.dimensions = this.tweenTargetScale;
		this.internalTweenInProgress = false;
		if (!this.canButtonBeHeldDown)
		{
			if (this._handlerScaleTweenSec <= 0f)
			{
				this.handler.dimensions = this.handlerSmallSize;
			}
			else
			{
				this.tweenTargetScale = this.handlerSmallSize;
				this.tweenStartingScale = this.handler.dimensions;
				base.StartCoroutine(this.ScaleTween());
				this.internalTweenInProgress = true;
			}
		}
		yield break;
	}

	private void InitData()
	{
		float x = Scene.GetRenderBound(this.handler.gameObject).size.x;
		float x2 = Scene.GetRenderBound(this.bgBack.gameObject).size.x;
		this.handlerPos = this.handler.transform.localPosition;
		this.handlerWidePosX = x2 - x * 2f - Mathf.Abs(this.handlerOffsetX) * 2f;
		this.handlerSmallPosX = x2 - x - this.handlerOffsetX;
		this.bgFront.gameObject.SetActive(this.showOffScaleTween);
		this.handlerSmallSize = this.handler.dimensions;
		this.handlerWideSize = new Vector2(this.handler.dimensions.x * 1.2f, this.handler.dimensions.y);
		this.tweenTargetScale = this.handler.dimensions;
		this.tweenStartingScale = this.handler.dimensions;
	}

	public void InternalSetUseOnReleaseInsteadOfOnUp(bool state)
	{
		this.useOnReleaseInsteadOfOnUp = state;
	}

	private const float SCALE_UP_TWEEN_MULTIPLE = 8f;

	private const float ANIMATION_SEC = 0.15f;

	private static Vector3 OUTOFSCREEN_POS = Vector3.one * -9999f;

	public bool canButtonBeHeldDown = true;

	[SerializeField]
	private bool useOnReleaseInsteadOfOnUp = true;

	private bool internalTweenInProgress;

	private float tweenTimeElapsed;

	public tk2dSprite bgFront;

	public tk2dSprite bgBack;

	public tk2dSlicedSprite handler;

	public Color frontBgColor = Color.white;

	public Color bgBackOffColor = Color.gray;

	public Color bgBackOnColor = new Color(0.298039228f, 0.843137264f, 0.392156869f, 1f);

	public bool showOffScaleTween = true;

	public bool useOnOverlay;

	public float handlerMovingTweenSec = 0.2f;

	public float handlerOffsetX = 0.1f;

	private Camera cam;

	private Vector2 handlerSmallSize;

	private Vector2 handlerWideSize;

	private Vector2 tweenTargetScale;

	private Vector2 tweenStartingScale;

	private Vector3 handlerPos;

	private Vector3 startTouchPos = IOSSwitch.OUTOFSCREEN_POS;

	private tk2dBaseSprite.Anchor anchorML = tk2dBaseSprite.Anchor.MiddleLeft;

	private tk2dBaseSprite.Anchor anchorMR = tk2dBaseSprite.Anchor.MiddleRight;

	private Tweener tweener;

	private bool isOn;

	private bool isPressed;

	private float handlerWidePosX;

	private float handlerSmallPosX;

	private float handlerScaleTweenSec = 0.05f;

	private float _handlerScaleTweenSec = 0.05f;

	private enum SWITCH_STATE
	{
		NOTHING,
		ON,
		OFF
	}
}
